//
//  selectMatchDialog.m
//  PlayBall
//
//  Created by Jdd‘s macbook on 13-10-8.
//
//

#import "JddselectMatchDialog.h"
#import <QuartzCore/QuartzCore.h>
#import "JddCommonFunction.h"
@implementation JddselectMatchDialog
GET_INSTANCE_VIEW_IMPLEMENT(JddselectMatchDialog)

- (void)loadContentView{
    _contentView = [JddselectMatchDialog getInstanceView];

    gridView.cellMargin = 8;
    gridView.numberOfRows = 4;
    gridView.numberOfColumns = 3;
    gridView.layoutStyle = VerticalLayout;
    
    
    bgImgView.layer.cornerRadius = 10;
    bgImgView.layer.masksToBounds = YES;
    
//    bg_top.layer.cornerRadius = 10;
//    bg_top.layer.masksToBounds = YES;
    
    CAShapeLayer *styleLayer = [CAShapeLayer layer];
    
    UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRoundedRect:bg_top.bounds byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(4.0, 4.0)];
    
    styleLayer.path = shadowPath.CGPath;
    
    bg_top.layer.mask = styleLayer;
     
    [self initData];
}

-(NSString *)subStringThreeWithName:(NSString *)name
{
    if (name.length > 3) {
        name = [name substringToIndex:3];
    }
    return name;
}

-(void)initData
{
    NSMutableDictionary *localDic = [[NSMutableDictionary alloc] init];
    for(NSDictionary *d in self.tableDataArray){
        NSString *name = [d objectForKey:@"i2"];
        
        NSArray *array = [localDic allKeys];
        if ([array containsObject:name])
            {
            NSNumber *count = [localDic objectForKey:name];
            int i = [count intValue] + 1;
            count = [NSNumber numberWithInt:i];
            [localDic setObject:count  forKey:name];
            }else{
                [localDic setObject:[NSNumber numberWithInt:1] forKey:name];
            }
    }
    localArray = [[NSMutableArray alloc] initWithCapacity:localDic.count];
    for(NSString *key in localDic.allKeys){
        NSString *count = [localDic objectForKey:key];
        //截取赛事名称
        NSString *content = [self subStringThreeWithName:[key copy]];
        content = [NSString stringWithFormat:@"%@(%@)", content, count];
        NSString *isSelected = [self getIsSelectedWithName:key];
        NSDictionary *contentDic = [NSDictionary dictionaryWithObject:isSelected forKey:content];
        [localArray addObject:contentDic];
    }
    [gridView reloadData];
}

-(NSString *)getIsSelectedWithName:(NSString *)name
{
    for(NSDictionary *d in self.tableDataArray)
        {
        if([[d objectForKey:@"i2"] isEqualToString:name]){
            return [d objectForKey:@"isSelected"];
        }
    }
    return @"1";
}

-(IBAction)touchOutside:(id)sender{
   // [self dismissAnimated:YES];
}

#pragma - MMGridViewDataSource

- (NSInteger)numberOfCellsInGridView:(MMGridView *)gridView
{
    return localArray.count;
}

- (JddScoreMatchCell *)gridView:(MMGridView *)gridView cellAtIndex:(NSUInteger)index
{
    JddScoreMatchCell *cell = [JddScoreMatchCell getInstanceView];
    cell.delegate = self;
    NSDictionary *contentDic = [localArray objectAtIndex:index];
    
    NSString *content = [contentDic.allKeys objectAtIndex:0];
    int isSelected = [[contentDic objectForKey:content] intValue];
    cell.btn.selected = isSelected;
    
    [cell.btn setTitle:content forState:UIControlStateNormal];
    return cell;
}

// ----------------------------------------------------------------------------------

#pragma - MMGridViewDelegate

- (void)didTouchBtn:(UIButton *)btn
{
    btn.selected = !btn.selected;
    NSArray *btnText = [btn.titleLabel.text componentsSeparatedByString:@"("];
    NSString *name = [btnText objectAtIndex:0];
    [self setIsSelectedWithName:name AndIsSelected:[NSString stringWithFormat:@"%d", btn.selected]];
}

-(void)setIsSelectedWithName:(NSString *)name AndIsSelected:(NSString *)isSelected
{
    NSMutableArray *tempArray = [self.tableDataArray mutableCopy];
    for(NSMutableDictionary *d in tempArray)
        {
        //截取赛事名称
        NSString *content = [self subStringThreeWithName:[[d objectForKey:@"i2"] copy]];
        if([content isEqualToString:name]){
            [d setValue:isSelected forKey:@"isSelected"];
            }
        }
    self.tableDataArray = tempArray;
    [self initData];
}

-(IBAction)selectOrDeSelectAllAction:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    NSMutableArray *tempArray = [self.tableDataArray mutableCopy];
    for(NSMutableDictionary *d in tempArray){
        [d setObject:[NSString stringWithFormat:@"%d", btn.tag] forKey:@"isSelected"];
    }
    self.tableDataArray = tempArray;
    [self initData];
}

-(BOOL)gameIsSelected
{
    for(NSDictionary *d in localArray)
        {
            NSArray *allValues = d.allValues;
            for(NSString *s in allValues){
                if ([s isEqualToString:@"1"]) {
                return YES;
                }
            }
        }
    return NO;
}

-(IBAction)sureAction:(id)sender
{
    if ([self gameIsSelected]) {
        [self.delegate performSelector:@selector(selectMatch:) withObject:localArray];
        [self dismissAnimated:YES];
        
    }else{
        [JddCommonFunction messageBoxWithMessage:@"至少选择一场比赛"];
    }
    
}

@end
